
//
//  NewApplyShowViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/5/8.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kItemWidth (App_Frame_Width - (15*5))/4
//#define kItemWidth 60

#import "NewApplyShowViewController.h"

#import "BHSelectMediaView.h"
#import "UIViewController+BHMediaExt.h"
#import "TZImagePickerController.h"
#import "BHSingleButton.h"
#import "NextStepViewController.h"

#import "DiquViewController.h"


@interface NewApplyShowViewController ()<PublicWelfareImageViewDataSource,TZImagePickerControllerDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UIButton *showImageBtn;
@property (nonatomic, strong) UIImage *showImage;
@property (nonatomic, strong) BHSelectMediaView *mediaView;
@property (nonatomic, strong) BHSingleButton *finishBtn;

@property (nonatomic, strong) NSMutableArray *imageArray;//实际数组
@property (nonatomic, strong) NSMutableArray *assetsArray;//记录临时数组

@property (nonatomic, strong) TZImagePickerController *imagePickController1;
@property (nonatomic, strong) TZImagePickerController *imagePickController5;


//@property (nonatomic, assign) CGFloat mediaViewHeight;

@end

@implementation NewApplyShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"填写资料";
    [self initNavBarBackBtn];
    
    [self setUpUI];
    
    self.showInfoArray = [[NSMutableArray alloc] initWithObjects:@"", @"", @"", @"", @"", nil];
    self.imageArray = [[NSMutableArray alloc] initWithCapacity:5];
    self.assetsArray = [[NSMutableArray alloc] initWithCapacity:5];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpUI {
//    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height-kNavbarAndStatusBar)];
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = Bg_Color_FFFFFF;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.width.equalTo(self.view);
    }];

    self.bgView = [[UIView alloc] init];
    //    self.bgView = [[UIView alloc] initWithFrame:self.scrollView.frame];
    self.bgView.backgroundColor = Bg_Color_FFFFFF;
    [self.scrollView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView.mas_left);
        make.right.equalTo(self.scrollView.mas_right);
        make.top.equalTo(self.scrollView.mas_top);
        make.bottom.equalTo(self.scrollView.mas_bottom);
        make.width.equalTo(self.scrollView);
    }];
    
    UILabel *showLabel = [[UILabel alloc] init];
    showLabel.text = @"参加展会";
    showLabel.font = BHFont_Size_15;
    showLabel.textColor = Text_Color_333333;
    [self.bgView addSubview:showLabel];
    [showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.top.equalTo(self.bgView.mas_top).offset(20);
        make.width.offset([BHSettingUtil widthForText:@"参加展会" fontSize:15]);
    }];
    
    UIImageView *nextIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home-7-1"]];
    [self.bgView addSubview:nextIV];
    [nextIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView.mas_right).offset(-20);
        make.centerY.equalTo(showLabel);
    }];
    
    UIButton *chooseBtn = [[UIButton alloc] init];
    [chooseBtn setTitle:@"" forState:UIControlStateNormal];
    [chooseBtn addTarget:self action:@selector(chooseShowInfoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:chooseBtn];
    [chooseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.centerY.equalTo(showLabel);
        make.height.offset(40);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = Bg_Color_EEEEEE;
    [self.bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left);
        make.height.offset(15);
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(showLabel.mas_bottom).offset(20);
    }];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"联系人：";
    nameLabel.font = BHFont_Size_15;
    nameLabel.textColor = Text_Color_333333;
    [self.bgView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.top.equalTo(line.mas_bottom).offset(20);
        make.width.offset([BHSettingUtil widthForText:@"联系人：" fontSize:15]);
    }];
    
    self.nameTF = [[UITextField alloc] init];
    self.nameTF.delegate = self;
    [self.bgView addSubview:self.nameTF];
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.centerY.equalTo(nameLabel);
        make.height.offset(40);
    }];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = Bottom_Line_Color;
    [self.bgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(nameLabel.mas_bottom).offset(15);
    }];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.text = @"联系人电话：";
    phoneLabel.font = BHFont_Size_15;
    phoneLabel.textColor = Text_Color_333333;
    [self.bgView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.top.equalTo(line1.mas_bottom).offset(20);
        make.width.offset([BHSettingUtil widthForText:@"联系人电话：" fontSize:15]);
    }];
    
    self.phoneTF = [[UITextField alloc] init];
    self.phoneTF.delegate = self;
    [self.bgView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_right);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.centerY.equalTo(phoneLabel);
        make.height.offset(40);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = Bottom_Line_Color;
    [self.bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(phoneLabel.mas_bottom).offset(15);
    }];
    
    UILabel *picLabel = [[UILabel alloc] init];
    picLabel.text = @"天鹅国际认证照片：";
    picLabel.font = BHFont_Size_15;
    picLabel.textColor = Text_Color_333333;
    [self.bgView addSubview:picLabel];
    [picLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.top.equalTo(line2.mas_bottom).offset(20);
        make.width.offset([BHSettingUtil widthForText:@"天鹅国际认证照片：" fontSize:15]);
    }];
    
    self.mediaView = [[BHSelectMediaView alloc]init];
    self.mediaView.dataSource = self;
    self.mediaView.maxImageCount = 5;
    self.mediaView.isDisplayDelete = YES;
    self.mediaView.isImplement = YES;
    [self.bgView addSubview:self.mediaView];
    [self.mediaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(picLabel.mas_bottom).offset(10);//20
        make.height.offset(kItemWidth*2+20);
//        make.height.offset(100);
    }];
    
    UILabel *pageLabel = [[UILabel alloc] init];
    pageLabel.text = @"上传合同照片：";
    pageLabel.font = BHFont_Size_15;
    pageLabel.textColor = Text_Color_333333;
    [self.bgView addSubview:pageLabel];
    [pageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.top.equalTo(self.mediaView.mas_bottom).offset(20);
        make.width.offset([BHSettingUtil widthForText:@"上传合同照片：" fontSize:15]);
    }];
    
    self.showImageBtn = [[UIButton alloc] init];
    [self.showImageBtn setImage:[UIImage imageNamed:@"my-4"] forState:UIControlStateNormal];
//    [self.showImageBtn setImage:self.showImage forState:UIControlStateNormal];
    [self.showImageBtn addTarget:self action:@selector(_selectPictureCamera:) forControlEvents:UIControlEventTouchUpInside];
    self.showImageBtn.layer.masksToBounds = YES;
    self.showImageBtn.layer.cornerRadius = 3;
    [self.bgView addSubview:self.showImageBtn];
    [self.showImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.height.width.offset(kItemWidth-10);
        make.top.equalTo(pageLabel.mas_bottom).offset(15);//18
    }];
    
    self.finishBtn = [[BHSingleButton alloc] init];
    self.finishBtn.button_name(@"提交申请");
    [self.finishBtn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    self.finishBtn.layer.masksToBounds = YES;
    self.finishBtn.layer.cornerRadius = 10;
    [self.bgView addSubview:self.finishBtn];
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(showLabel.mas_left);
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(self.showImageBtn.mas_bottom).offset(50);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-50);//70
        make.height.offset(60);//56
        
//        make.bottom.equalTo(self.view.mas_bottom).offset(50);//70
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.frame = CGRectMake(0, 0, App_Frame_Width, CGRectGetHeight(self.view.bounds));
}

#pragma mark - PublicWelfareImageViewDataSource
- (CGSize)collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kItemWidth, kItemWidth);
}

- (NSArray *)dataArraynumberOfItems:(BHSelectMediaView *)mediaView {
    return self.imageArray;
}

- (void)deleteSelectMediaView:(BHSelectMediaView *)mediaView deleteItem:(NSInteger)item {
    [self.imageArray removeObjectAtIndex:item];
    [self.assetsArray removeObjectAtIndex:item];
    [self _changeViewFormImageCount];
}

- (void)deleteAddImage:(BHSelectMediaView *)mediaView {
    [self _selectPictureCamera:mediaView];
}

#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    if (picker == self.imagePickController1) {
        self.showImage = photos[0];
        [self.showImageBtn setImage:[BHSettingUtil dataAndStringIsNull:self.showImage]?[UIImage imageNamed:@"my-4"]:self.showImage forState:UIControlStateNormal];
    } else {
        [self.imageArray removeAllObjects];
        [self.assetsArray removeAllObjects];
        [self.imageArray addObjectsFromArray:photos];
        [self.assetsArray addObjectsFromArray:assets];
        [self _changeViewFormImageCount];
    }
}


#pragma mark -
- (void)_selectPictureCamera:(id)sender {
    [UIViewController showAlertWithTitle:nil message:nil actionTitles:@[@"相册"] cancelTitle:@"取消" style:UIAlertControllerStyleActionSheet completion:^(NSInteger index) {
        if (index == 0) {
            if ([sender isKindOfClass:[UIButton class]]) {
                self.imagePickController1 = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
                self.imagePickController1.allowPickingVideo = NO;
//                imagePickController.selectedAssets = self.assetsArray;
                [self presentViewController:self.imagePickController1 animated:YES completion:nil];
            } else {
                self.imagePickController5 = [[TZImagePickerController alloc] initWithMaxImagesCount:5 delegate:self];
                self.imagePickController5.allowPickingVideo = NO;
                self.imagePickController5.selectedAssets = self.assetsArray;
                [self presentViewController:self.imagePickController5 animated:YES completion:nil];
            }
        }
    }];
}

- (void)_changeViewFormImageCount {
//    if (self.imageArray.count > 3) {
//        self.mediaViewHeight = 180;
//    } else {
//        self.mediaViewHeight = 100;
//    }
    [self.mediaView reloadDataSelectMediaView];
}

#pragma mark 按钮事件

- (void)chooseShowInfoAction {
    DiquViewController *next = [[DiquViewController alloc] init];
//    next.Id = @"3";
//    NextStepViewController *next = [[NextStepViewController alloc] init];
//    next.title = @"地区选择";
//    next.page = 1;
//    next.chooseArray = [[NSMutableArray alloc] initWithObjects:@"", @"", @"", @"", @"", nil];
    [self pushViewController:next animated:YES];
}

- (void)finishAction {
    if([self.showInfoArray containsObject:@""]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请选择参加展会"];
    }
    else if([BHSettingUtil dataAndStringIsNull:self.nameTF.text]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请输入联系人"];
    }
    else if([BHSettingUtil dataAndStringIsNull:self.phoneTF.text]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请输入联系人电话"];
    }
    else if(self.assetsArray.count == 0) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请上传展位照片"];
    }
    else if([BHSettingUtil dataAndStringIsNull:self.showImage]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请上传合同照片"];
    }
    else {
        [self requestPostBH_User_setUserBooth];
    }
}

- (void)requestPostBH_User_setUserBooth {
    SGUserModel *userModel = [SGUserInfo getUserInfo];
    
    [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    [params setObject:@"14" forKey:@"company_id"];
    if (![BHSettingUtil dataAndStringIsNull:userModel.Id]) {
        [params setObject:userModel.Id forKey:@"uid"];
    }
    else {
        
    }
    //"title": "杭州西博春季博览会",
    [params setObject:self.showInfoArray[2] forKey:@"exhibition_id"];//展会ID
    [params setObject:self.showInfoArray[0] forKey:@"area_id"];//区域ID
    [params setObject:self.phoneTF.text forKey:@"phone"];
    [params setObject:self.nameTF.text forKey:@"contacts"];
    [params setObject:self.showInfoArray[4] forKey:@"booth_name"];//展位号
    
//    NSArray *nameArray = @[@"auth_img",@"booth_img",@"pact_img"];
//    NSArray *picArray = @[@[self.imageArray[0]], @[self.imageArray[1], self.imageArray[2]], @[self.imageArray[3]]];
    NSArray *nameArray = @[@"booth_img",@"pact_img"];
    NSArray *picArray = @[self.imageArray, @[self.showImage]];
//    [params setObject:self.productTF.text forKey:@"auth_img"];//推广认证照片
//    [params setObject:self.productTF.text forKey:@"booth_img"];//展位照片
//    [params setObject:self.productTF.text forKey:@"pact_img"];//合同照片
    
    [PPHTTPRequest requestPostBH_User_setUserBooth:^(id response, BOOL success) {
        if (success) {
            [[BHAlertUtil alertManager] showPromptInfo:@"新增成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            //            @WeakObj(self);
            //            [self.cycleView.placeholderShow(YES) actionWithBlock:^{
            //                [selfWeak requestPostBH_Exhibition_getPicByBoothId];
            //            }];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
        
    } params:params imagesArray:picArray names:nameArray];
}

@end
